package cn.com.yiqi.wsc.portal.controller.customer;

import cn.com.yiqi.wsc.Ro.CustomerRo;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.dao.balance.criteria.BalanceCriteria;
import cn.com.yiqi.wsc.dao.customer.criteria.CustomerCriteria;
import cn.com.yiqi.wsc.dao.customer.criteria.CustomerListCriteria;
import cn.com.yiqi.wsc.dao.order.criteria.OrderRecordCriteria;
import cn.com.yiqi.wsc.dao.point.criteria.PointCriteria;
import cn.com.yiqi.wsc.dao.point.entity.PointRecordEntity;
import cn.com.yiqi.wsc.entity.item.ItemDTO;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.customer.PortalCutomerService;
import cn.com.yiqi.wsc.service.customer.impl.PortalCustomerImpl;
import cn.com.yiqi.wsc.service.outputExcel.OutPutService;
import cn.com.yiqi.wsc.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by 苟宇恒 on 2017/2/22.
 * 客户管理
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {


    @Autowired
    PortalCutomerService portalCustomer;
    @Autowired
    private OutPutService outPutService;



    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("customer/index");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView("customer/detail");
        this.getUserData(mv);
        mv.addObject("customerId",id);
        return mv;
    }
    /**
     * 用户列表
     */

    @RequestMapping(value = "/customerList")
    @ResponseBody
    public Result findCustomerList(@RequestBody CustomerListCriteria customerListCriteria){
        return portalCustomer.findCustomerList(customerListCriteria);

    }

    @RequestMapping("/updatEnable")
    @ResponseBody
    public Result updateCustomerStatus(@RequestBody CustomerCriteria  customerCriteria){
        return portalCustomer.updateCustomerStatus(customerCriteria);
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Result deleteCustomer(@RequestBody CustomerCriteria  customerCriteria){
        return portalCustomer.deleteCustomer(customerCriteria);
    }

    @RequestMapping("/oneCustomer")
    @ResponseBody
    public Result findCustomerAndHigherCustomer(@RequestBody CustomerCriteria  customerCriteria){
        return portalCustomer.findCustomerAndHigherCustomer(customerCriteria.getCustomerId());
    }

    @RequestMapping("/info")
    @ResponseBody
    public Result findCustomerInfo(@RequestBody CustomerCriteria  customerCriteria){
        return portalCustomer.findCustomerInfo(customerCriteria.getCustomerId());
    }

    @RequestMapping("/address")
    @ResponseBody
    public Result findCustomerAddress(@RequestBody  CustomerCriteria customerCriteria){
        return portalCustomer.findCustomerAddress(customerCriteria);
    }

    @RequestMapping("/mentions")
    @ResponseBody
    public Result findCustomerMentions(@RequestBody  CustomerCriteria customerCriteria){
        return portalCustomer.findCustomerMentions(customerCriteria);
    }

    @RequestMapping("/points")
    @ResponseBody
    public Result findCustomerPoints(@RequestBody  PointCriteria pointCriteria){
        return portalCustomer.findCustomerPointDetail(pointCriteria);
    }

    @RequestMapping("/balance")
    @ResponseBody
    public Result findCustomerBalance(@RequestBody  BalanceCriteria balanceCriteria){
        return portalCustomer.findCustomerBalanceDetail(balanceCriteria);
    }
    @RequestMapping("/balanceFetch")
    @ResponseBody
    public Result findCustomerBalanceFetch(@RequestBody  BalanceCriteria balanceCriteria){
        return portalCustomer.findCustomerBalanceFetchDetail(balanceCriteria);
    }
    @RequestMapping("/signed")
    @ResponseBody
    public Result findCustomerSigned(@RequestBody  CustomerCriteria customerCriteria){
        return portalCustomer.findCustomerSignedDetail(customerCriteria);
    }
    @RequestMapping("/lottery")
    @ResponseBody
    public Result findCustomerLottery(@RequestBody  CustomerCriteria customerCriteria){
        return portalCustomer.findCustomerLotteryDetail(customerCriteria);
    }
    @RequestMapping("/order")
    @ResponseBody
    public Result findCustomerOrder(@RequestBody  OrderRecordCriteria orderRecordCriteria){
        return portalCustomer.findCustomerOrderDetail(orderRecordCriteria);
    }
    @RequestMapping("/favor")
    @ResponseBody
    public Result findCustomerFavor(@RequestBody  CustomerCriteria customerCriteria){
        return portalCustomer.findCustomerFavorDetail(customerCriteria);
    }
    @RequestMapping("/cart")
    @ResponseBody
    public Result findCustomerCart(@RequestBody  CustomerCriteria customerCriteria){
        return portalCustomer.findCustomerCartDetail(customerCriteria);
    }
    @RequestMapping("/distribution")
    @ResponseBody
    public Result findCustomerDistribution(@RequestBody  CustomerListCriteria customerListCriteria){
        return portalCustomer.findCustomerDistributionList(customerListCriteria);
    }

    @RequestMapping("/addPresentPoint")
    @ResponseBody
    public Result insertPresentPoint(@RequestBody  PointRecordEntity pointRecordEntity){
        return portalCustomer.insertPresentPoint(pointRecordEntity);
    }

    @RequestMapping("/updateCustomerParam")
    @ResponseBody
    public Result updateCustomerParam(@RequestBody  CustomerRo customerRo){
        return portalCustomer.updateCustomerParam(customerRo);
    }

    @RequestMapping("/outPut")
    public void outPut( @RequestBody CustomerListCriteria customerListCriteria ,HttpServletRequest request, HttpServletResponse response) {
        customerListCriteria.setPageSize(500000);
        List<CustomerRo> list = (List<CustomerRo>) portalCustomer.findCustomerList(customerListCriteria).getData().get("customerList");
        String title = "客户列表";
        String[] hearders = new String[] {"姓名", "昵称", "状态(1启用0禁用)", "手机号码", "性别(1男性2女性0未知)", "最后登录时间","余额","总消费金额(￥)","积分","是否会员(1是0否)"};//表头数组
        String[] fields = new String[] {"name", "nick", "enable", "tel", "sex","lastLoginTime", "balance","dealAmount","point","vip"};//People对象属性数组
        String creater = userService.getCurrentUser().getRealName();
        try {
            outPutService.outPutExcel(list,title,hearders,fields,creater,request,response);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
